﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using DLV.Data.Entities;
using DLV.Library.Utilities;
using System.Globalization;

namespace DLV.Services.HttpHandlers
{
    public class AjaxHandler_hc : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            #region Address
            if (context.Request["function"] == "ListNationalByContinent" && !String.IsNullOrEmpty(context.Request["ContinentId"]))
            {
                var continentId = int.Parse(context.Request["ContinentId"]);
                ListNationalByContinent(context, continentId);
            }
            else if (context.Request["function"] == "ListCityByNational" && !String.IsNullOrEmpty(context.Request["NationalId"]))
            {
                var nationalId = int.Parse(context.Request["NationalId"]);
                ListCityByNational(context, nationalId);
            }
            else if (context.Request["function"] == "ListDistrictByCity" && !String.IsNullOrEmpty(context.Request["CityId"]))
            {
                var cityId = int.Parse(context.Request["CityId"]);
                ListDistrictByCity(context, cityId);
            }
            else if (context.Request["function"] == "ListWardByDistrict" && !String.IsNullOrEmpty(context.Request["DistrictId"]))
            {
                var districtId = int.Parse(context.Request["DistrictId"]);
                ListWardByDistrict(context, districtId);
            }
            #endregion

            #region TravelGuidePrice
            else if (!String.IsNullOrEmpty(context.Request["function"]) && context.Request["function"] == "ListTravelGuidePriceByTravelGuide")
            {
                var travelGuideId = int.Parse(context.Request["TravelGuideId"]);
                ListTravelGuidePriceByTravelGuide(context, travelGuideId);
            }
            #endregion

            #region Price
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                     context.Request["function"] == "ListConcernByType")
            {
                var typeId = int.Parse(context.Request["TypeId"]);
                switch (typeId)
                {
                    case (int)Globals.PriceType.TicketAir:
                        ListConcern(context);
                        break;
                    case (int)Globals.PriceType.TicketStation:
                        ListConcern(context);
                        break;
                    case (int)Globals.PriceType.TicketCar:
                        ListConcern(context);
                        break;
                    case (int)Globals.PriceType.Insurance:
                        ListPartner(context);
                        break;
                    case (int)Globals.PriceType.Service:
                        ListService(context);
                        break;
                }
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                     context.Request["function"] == "TicketListByConcernAndType")
            {
                var typeId = int.Parse(context.Request["TypeId"]);
                var concernId = int.Parse(context.Request["ConcernId"]);
                var hotelId = int.Parse(context.Request["ConcernId"]);

                if (typeId == (int)Globals.PriceType.TicketAir
                    || typeId == (int)Globals.PriceType.TicketStation
                    || typeId == (int)Globals.PriceType.TicketCar)
                    TicketListByConcernAndType(context, typeId, concernId);
                else if (typeId == (int)Globals.PriceType.Insurance)
                {
                    ListInsuranceByPartner(context, concernId);
                }
                else if (typeId == (int)Globals.PriceType.Hotel) { ListRoomByHotel(context, hotelId); }
                //else if (typeId == (int) Globals.PriceType.Service)
                //{
                //    ListServiceByLocation(context, concernId);
                //}
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                 context.Request["function"] == "ListServiceByPartnerAndLocation")
            {
                var typeId = int.Parse(context.Request["TypeId"]);
                var partnerId = int.Parse(context.Request["PartnerId"]);
                var locationId = int.Parse(context.Request["LocationId"]);


                ListServiceByLocation(context, partnerId, locationId);

            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
             context.Request["function"] == "ListMenuByRestaurantAndMenuType")
            {
                var typeId = int.Parse(context.Request["TypeId"]);
                var restaurantId = int.Parse(context.Request["RestaurantId"]);
                var menuTypeId = int.Parse(context.Request["MenuTypeId"]);


                ListMenuByRestaurantAndMenuType(context, restaurantId, menuTypeId);

            }
            #endregion

            #region PriceItem
            
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                     context.Request["function"] == "ListPriceByType")
            {
                var typeId = int.Parse(context.Request["TypeId"]);
                ListPriceByType(context, typeId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                 context.Request["function"] == "PriceListByShedule")
            {
                var scheduleId = int.Parse(context.Request["ScheduleId"]);
                var type = int.Parse(context.Request["TypeId"]);
                ListPriceBySchedule(context, type, scheduleId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "LocationListByScheduleType")
            {
                var scheduleType = int.Parse(context.Request["ScheduleType"]);
                ListLocationByScheduleType(context, scheduleType);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "ListLocationByLocationType")
            {
                var locationType = int.Parse(context.Request["LocationType"]);
                ListLocationByLocationType(context, locationType);
            }


            #endregion

            #region Upload Image Ajax

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                     context.Request["function"].Equals("UploadImageAjax"))
            {
                var imageEncode = context.Request["imageData"];
                var imageData = ImageUpload.Base64ToImage(imageEncode);
                var imageSize = int.Parse(context.Request["imageSize"]);
                UploadImageAjax(context, imageData, imageSize);
            }

            #endregion
            #region Stay
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                     context.Request["function"] == "ListPriceByHotel")
            {
                var hotelId = int.Parse(context.Request["HotelId"]);
                ListPriceByHotelAndType(context, hotelId);
            }
            #endregion

            #region TourService
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "ListCityandConcernByNational")
            {
                var nationalId = int.Parse(context.Request["NationalId"]);
                ListCityandPartnerByNational(context, nationalId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "ListHotelByHotelType")
            {
                var hotelTypeId = int.Parse(context.Request["HotelTypeId"]);
                var cityId = int.Parse(context.Request["CityId"]);
                ListHotelByHotelType(context, hotelTypeId, cityId);
            }

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "ListRoomTypeByHotel")
            {
                var hotelId = int.Parse(context.Request["HotelId"]);
                var season = context.Request["Season"];
                ListRoomTypeByHotel(context, hotelId, season);
            }

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "ListRestaurantByRestaurantType")
            {
                var restaurantTypeId = int.Parse(context.Request["RestaurantTypeId"]);
                var cityId = int.Parse(context.Request["CityId"]);
                ListRestaurantByRestaurantType(context, restaurantTypeId, cityId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
        context.Request["function"] == "ListLocationByCity")
            {
                var cityId = int.Parse(context.Request["CityId"]);
                loadLocationByCity(context, cityId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "ListVisaByPartner")
            {
                var partnerId = int.Parse(context.Request["PartnerId"]);
                ListVisaByPartner(context, partnerId);
            }
            #endregion

            #region chiet tinh may bay
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "PriceTicKetAri")
            {
                var priceId = int.Parse(context.Request["TicketId"]);
                GetPrice(context, priceId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "PriceListBy")
            {
                var type = int.Parse(context.Request["TypeId"]);
                var scheduleId = int.Parse(context.Request["ScheduleId"]);
                var date = DateTime.Parse(context.Request["Date"]);
                var flightArrival = context.Request["FlightArrival"];
                var concernId = int.Parse(context.Request["ConcernId"]);
                var season = context.Request["Season"];
                ListPriceBy(context, type, scheduleId, date, flightArrival, concernId, season);


            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "InsertAir")
            {
                try
                {
                    var calculatorInfo = new CalculatorInfo();
                    calculatorInfo.ItemType = int.Parse(context.Request["TypeId"]);
                    calculatorInfo.TourId = int.Parse(context.Request["TourId"]);
                    calculatorInfo.ItemId = int.Parse(context.Request["TicKetId"]);
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["PriceAdult"]);
                    calculatorInfo.PriceChild = decimal.Parse(context.Request["PriceChild"]);
                    calculatorInfo.DateBegin = DateTime.Parse(context.Request["Date"]);
                    calculatorInfo.DateEnd = DateTime.Parse(context.Request["Date"]);
                    calculatorInfo.QuantityCar = 0;
                    calculatorInfo.QuantityAdults = 0;
                    calculatorInfo.QuantityChild = 0;
                    calculatorInfo.Vat = double.Parse(context.Request["VatAir"]);

                    var quantityAdult = int.Parse(context.Request["QuantityAdult"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertAir(context, calculatorInfo, quantityAdult, quantityChild, calculatorId, className);
                }
                catch
                {
                    var calculatorInfo = new CalculatorInfo();
                    var quantityAdult = int.Parse(context.Request["QuantityAdult"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertAir(context, calculatorInfo, quantityAdult, quantityChild, calculatorId, className);
                }

            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "UpdateAir")
            {

                var calculatorId = int.Parse(context.Request["CalculatorId"]);
                GetTicketAir(context, calculatorId);
            } 

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "InsertStation")
            {
                try
                {
                    var calculatorInfo = new CalculatorInfo();
                    calculatorInfo.ItemType = int.Parse(context.Request["TypeId"]);
                    calculatorInfo.TourId = int.Parse(context.Request["TourId"]);
                    calculatorInfo.ItemId = int.Parse(context.Request["TicketId"]);
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["PriceAdult"]);
                    calculatorInfo.PriceChild = decimal.Parse(context.Request["PriceChild"]);
                    calculatorInfo.DateBegin = DateTime.Parse(context.Request["Date"]);
                    calculatorInfo.DateEnd = DateTime.Parse(context.Request["Date"]);
                    calculatorInfo.QuantityAdults = int.Parse(context.Request["QuantityAdult"]);
                    calculatorInfo.QuantityChild = int.Parse(context.Request["QuantityChild"]);
                    calculatorInfo.Vat = double.Parse(context.Request["VatStation"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertStation(context, calculatorInfo, calculatorId, className);
                }
                catch
                {
                    var calculatorInfo = new CalculatorInfo();
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertStation(context, calculatorInfo, calculatorId, className);
                }
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "UpdateStation")
            {

                var calculatorId = int.Parse(context.Request["CalculatorId"]);
                GetTicketStation(context, calculatorId);
            }
            #endregion

            #region chiet tinh Car
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
           context.Request["function"] == "PriceListByCar")
            {
                var type = int.Parse(context.Request["TypeId"]);
                var scheduleCar = int.Parse(context.Request["ScheduleCar"]);
                var date = DateTime.Parse(context.Request["DateCar"]);
                var soChoCar = context.Request["SoChoCar"];
                var concernCar = int.Parse(context.Request["ConcernCar"]);
                var yearCar = int.Parse(context.Request["YearCar"]);
                var seasionCar = context.Request["SeasionCar"];
                ListPriceByCar(context, type, scheduleCar, date, soChoCar, concernCar, yearCar, seasionCar);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "InsertCar")
            {
                try
                {
                    var calculatorInfo = new CalculatorInfo();
                    calculatorInfo.ItemType = int.Parse(context.Request["TypeId"]);
                    calculatorInfo.TourId = int.Parse(context.Request["TourId"]);
                    calculatorInfo.ItemId = int.Parse(context.Request["TicKetId"]);
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["PriceAdult"]);
                    calculatorInfo.PriceChild = 0;
                    calculatorInfo.DateBegin = DateTime.Parse(context.Request["Date"]);
                    calculatorInfo.DateEnd = DateTime.Parse(context.Request["Date"]);
                    calculatorInfo.QuantityCar = int.Parse(context.Request["QuantityCar"]);
                    calculatorInfo.QuantityAdults = 0;
                    calculatorInfo.QuantityChild = 0;
                    calculatorInfo.Vat = double.Parse(context.Request["VatCar"]);

                    var quantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertCar(context, calculatorInfo, calculatorId, className, quantityAdults, quantityChild);
                }
                catch
                {
                    var calculatorInfo = new CalculatorInfo();
                    var quantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertCar(context, calculatorInfo, calculatorId, className, quantityAdults, quantityChild);
                }

            }
          

         
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "UpdateCar")
            {

                var calculatorId = int.Parse(context.Request["CalculatorId"]);
                GetTicketCar(context, calculatorId);
            }

            #endregion

            #region chiet tinh khach san

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "PriceRoom")
            {

                int roomTypeId = int.Parse(context.Request["RoomId"]);
                DateTime dateBegin = DateTime.Parse(context.Request["DateBegin"]);
                PriceRoom(context, roomTypeId, dateBegin);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&

          context.Request["function"] == "GetPrice")
            {
                var priceId = int.Parse(context.Request["priceId"]);
                GetPrice(context, priceId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&

               context.Request["function"] == "UpdateCalculatorHotel")
            {
                int calculatorId = int.Parse(context.Request["CalculatorId"]);
                UpdateCalculatorHotel(context, calculatorId);
            }

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "DeleteCalculatorHotel")
            {

                int calculatorId = int.Parse(context.Request["Calculatorid"]);
                var calculatorController = new CalculatorController();
                calculatorController.DeleteCalculator(calculatorId);

            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                     context.Request["function"] == "InsertCalculatorHotel")
            {
                try
                {
                    var calculatorInfo = new CalculatorInfo();
                    calculatorInfo.TourId = int.Parse(context.Request["TourId"]);
                    calculatorInfo.ItemType = int.Parse(context.Request["TypeId"]);
                    calculatorInfo.ItemId = int.Parse(context.Request["RoomId"]);
                    calculatorInfo.DateBegin = DateTime.Parse(context.Request["DateBegin"]);
                    calculatorInfo.DateEnd = DateTime.Parse(context.Request["DateEnd"]);
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["PriceAdult"]);
                    calculatorInfo.PriceChild = decimal.Parse(context.Request["PriceChild"]);
                    calculatorInfo.QuantityAdults = int.Parse(context.Request["QuantityAdult"]);
                    calculatorInfo.QuantityChild = int.Parse(context.Request["QuantityChild"]);
                    calculatorInfo.Vat = double.Parse(context.Request["VAT"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertCalculatorHotel(context, calculatorInfo, calculatorId, className);
                }
                catch
                {
                    var calculatorInfo = new CalculatorInfo();
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertCalculatorHotel(context, calculatorInfo, calculatorId, className);
                }

            }
            #endregion

            #region  chiet tinh tour
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
               context.Request["function"] == "UpdateTour")
            {

                var tourId = int.Parse(context.Request["TourId"]);
                //UpdateTour(context, tourId);
            }
            #endregion

            #region chiet tinh an uong
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
       context.Request["function"] == "InsertMenu")
            {
                try
                {
                    var calculatorInfo = new CalculatorInfo();
                    calculatorInfo.ItemType = int.Parse(context.Request["TypeId"]);
                    calculatorInfo.TourId = int.Parse(context.Request["TourId"]);
                    calculatorInfo.ItemId = int.Parse(context.Request["menuElementID"]);
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["PriceAdults"]);
                    calculatorInfo.PriceChild = decimal.Parse(context.Request["PriceChilds"]);
                    calculatorInfo.DateBegin = DateTime.Parse(context.Request["Date"]);
                    //calculatorInfo.DateEnd = null;
                    calculatorInfo.QuantityCar = 0;
                    calculatorInfo.QuantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    calculatorInfo.QuantityChild = int.Parse(context.Request["QuantityChild"]);
                    calculatorInfo.QuantityBaby = int.Parse(context.Request["QuantityBaby"]);
                    calculatorInfo.PriceBaby = int.Parse(context.Request["PriceBaby"]);
                    calculatorInfo.Vat = double.Parse(context.Request["Vat"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertMenu(context, calculatorInfo, calculatorId, className);
                }
                catch
                {
                    var calculatorInfo = new CalculatorInfo();
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertMenu(context, calculatorInfo, calculatorId, className);
                }

            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "PriceListByMenu")
            {

                var typeId = int.Parse(context.Request["TypeId"]);
                var restaurantId = int.Parse(context.Request["RestaurantId"]);
                var menuTypeId = int.Parse(context.Request["MenuTypeId"]);
                var seasonTour = context.Request["SeasonTour"];
                var date = DateTime.Parse(context.Request["Date"]);
                PriceListByMenu(context, typeId, restaurantId, menuTypeId, seasonTour, date);
            }

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "UpdateMenu")
            {

                var calculatorId = int.Parse(context.Request["CalculatorId"]);
                GetTicketMenu(context, calculatorId);
            }
            #endregion


            #region chiet tinh dich vu khac

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "PriceListByDvk")
            { 
                var typeId = int.Parse(context.Request["TypeId"]);
                var partnerId = int.Parse(context.Request["PartnerId"]);
                var locationId = int.Parse(context.Request["LocationId"]);
                var seasonTour = context.Request["SeasonTour"];

                PriceListByDvk(context, typeId, partnerId, locationId, seasonTour);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
           context.Request["function"] == "PriceDvk")
            {
                var priceId = int.Parse(context.Request["ServiceId"]);
                GetPrice(context, priceId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "InsertDvk")
            {
                try
                {
                    var calculatorInfo = new CalculatorInfo();
                    calculatorInfo.ItemType = int.Parse(context.Request["TypeId"]);
                    calculatorInfo.TourId = int.Parse(context.Request["TourId"]);
                    calculatorInfo.ItemId = int.Parse(context.Request["menuElementID"]);
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["PriceAdults"]);
                    calculatorInfo.PriceChild = decimal.Parse(context.Request["PriceChilds"]);
                    calculatorInfo.DateBegin = DateTime.Parse(context.Request["Date"]);
                    //calculatorInfo.DateEnd = null;
                    calculatorInfo.QuantityCar = 0;
                    calculatorInfo.QuantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    calculatorInfo.QuantityChild = int.Parse(context.Request["QuantityChild"]);
                    calculatorInfo.PriceBaby = int.Parse(context.Request["PriceBaby"]);
                    calculatorInfo.QuantityBaby = int.Parse(context.Request["QuantityBaby"]);
                    calculatorInfo.Vat = double.Parse(context.Request["Vat"]);

                    var quantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertDvk(context, calculatorInfo, calculatorId, className, quantityAdults, quantityChild);
                }
                catch
                {
                    var calculatorInfo = new CalculatorInfo();
                    var quantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertDvk(context, calculatorInfo, calculatorId, className, quantityAdults, quantityChild);
                }

            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "UpdateDvk")
            {

                var calculatorId = int.Parse(context.Request["CalculatorId"]);
                UpdateDvk(context, calculatorId);
            }
            #endregion

            #region chiet tinh hoa hong

           // else if (!String.IsNullOrEmpty(context.Request["function"]) &&
           //     context.Request["function"] == "PriceListByDvk")
           // {
           //     var typeId = int.Parse(context.Request["TypeId"]);
           //     var partnerId = int.Parse(context.Request["PartnerId"]);
           //     var locationId = int.Parse(context.Request["LocationId"]);
           //     var seasonTour = context.Request["SeasonTour"];

           //     PriceListByDvk(context, typeId, partnerId, locationId, seasonTour);
           // }
           // else if (!String.IsNullOrEmpty(context.Request["function"]) &&
           //context.Request["function"] == "PriceDvk")
           // {
           //     var priceId = int.Parse(context.Request["ServiceId"]);
           //     GetPrice(context, priceId);
           // }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "InsertHH")
            {
                try
                {
                    var calculatorInfo = new CalculatorInfo();
                    calculatorInfo.ItemType = int.Parse(context.Request["TypeId"]);
                    calculatorInfo.TourId = int.Parse(context.Request["TourId"]);
                    calculatorInfo.TravelGuideType =  context.Request["TravelGuideType"] ;
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["PriceAdults"]);
                    calculatorInfo.PriceChild = decimal.Parse(context.Request["PriceChilds"]);
 
                    calculatorInfo.QuantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    calculatorInfo.QuantityChild = int.Parse(context.Request["QuantityChild"]);

                    calculatorInfo.QuantityBaby = int.Parse(context.Request["QuantityBaby"]);
                    calculatorInfo.PriceBaby = int.Parse(context.Request["PriceBaby"]);
                    calculatorInfo.Vat = double.Parse(context.Request["Vat"]);

                    var quantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertHH(context, calculatorInfo, calculatorId, className, quantityAdults, quantityChild);
                }
                catch
                {
                    var calculatorInfo = new CalculatorInfo();
                    var quantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertHH(context, calculatorInfo, calculatorId, className, quantityAdults, quantityChild);
                }

            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "UpdateHH")
            {

                var calculatorId = int.Parse(context.Request["CalculatorId"]);
                UpdateHH(context, calculatorId);
                 }
            #endregion


            #region chiet tinh HDV
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
               context.Request["function"] == "TravelGuideListBy")
            {

                var language = int.Parse(context.Request["language"]);
                var gender = context.Request["gender"];
                var level = context.Request["level"];

                ListTravelGuideBy(context, language, gender, level);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
              context.Request["function"] == "GetTravelGuide")
            {

                var travelGuideId = int.Parse(context.Request["travelGuideId"]);


                GetTravelGuide(context, travelGuideId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
             context.Request["function"] == "InsertTravelGuide")
            {
                var calculatorId = int.Parse(context.Request["calculatorId"]);
                var className = context.Request["className"];

                var calculatorInfo = new CalculatorInfo();
                try
                {
                    calculatorInfo.TourId = int.Parse(context.Request["tourId"]);
                    calculatorInfo.ItemId = int.Parse(context.Request["ticketId"]);
                    calculatorInfo.ItemType = int.Parse(context.Request["typeId"]);
                    calculatorInfo.DateBegin = DateTime.Parse(context.Request["beginDate"]);
                    calculatorInfo.DateEnd = DateTime.Parse(context.Request["endDate"]);
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["priceAdult"]);
                    calculatorInfo.PriceChild = decimal.Parse(context.Request["priceChild"]);
                    calculatorInfo.Vat = double.Parse(context.Request["vat"]);
                    calculatorInfo.TravelGuideType = context.Request["travelGuideType"];
                    calculatorInfo.QuantityCar = int.Parse(context.Request["language"]);
                }
                catch { }
                var quantityAdults = int.Parse(context.Request["QuantityAdults"]);
                var quantityChild = int.Parse(context.Request["QuantityChild"]);
                InsertTravelGuide(context, calculatorInfo, calculatorId, className, quantityAdults, quantityChild);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
               context.Request["function"] == "UpdateTravelGuide")
            {
                var calculatorId = int.Parse(context.Request["CalculatorId"]);
                UpdateTravelGuide(context, calculatorId);

            }
            #endregion

            #region chiet tinh bao hiem
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "InsuranceByPartner")
            {
                var partnerId = int.Parse(context.Request["PartnerId"]);
                ListInsuranceByPartner(context, partnerId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "PriceInsurance")
            {
                var insuranceId = int.Parse(context.Request["InsuranceId"]);
                DateTime dateBegin = DateTime.Parse(context.Request["DateBegin"]);
                PriceInsurance(context, insuranceId, dateBegin);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                    context.Request["function"] == "InsertInsurance")
            {


                try
                {
                    var calculatorInfo = new CalculatorInfo();
                    calculatorInfo.TourId = int.Parse(context.Request["TourId"]);
                    calculatorInfo.ItemType = int.Parse(context.Request["TypeId"]);
                    calculatorInfo.ItemId = int.Parse(context.Request["TicketId"]);
                    calculatorInfo.DateBegin = DateTime.Parse(context.Request["DateBegin"]);
                    calculatorInfo.DateEnd = DateTime.Parse(context.Request["DateEnd"]);
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["PriceAdult"]);
                    calculatorInfo.PriceChild = decimal.Parse(context.Request["PriceChild"]);
                    calculatorInfo.QuantityAdults = int.Parse(context.Request["QuantityAdult"]);
                    calculatorInfo.QuantityChild = int.Parse(context.Request["QuantityChild"]);
                    calculatorInfo.Vat = double.Parse(context.Request["VAT"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertCalculatorInsurance(context, calculatorInfo, calculatorId, className);
                }
                catch
                {
                    var calculatorInfo = new CalculatorInfo();
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertCalculatorInsurance(context, calculatorInfo, calculatorId, className);
                }

            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "UpdateCalculatorInsurance")
            {
                int calculatorId = int.Parse(context.Request["CalculatorId"]);
                UpdateCalculatorInsurance(context, calculatorId);
            }
            #endregion

            #region chiet tinh Visa

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "PriceListByVisa")
            {

                var partnerId = int.Parse(context.Request["PartnerId"]);
                var visaTypeId = int.Parse(context.Request["VisaTypeId"]);

                PriceListByVisa(context, partnerId, visaTypeId);
            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
                context.Request["function"] == "PriceVisa")
            {
                var priceVisaId = int.Parse(context.Request["PriceVisaId"]);
                GetVisaPrice(context, priceVisaId);
            }

            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
            context.Request["function"] == "UpdateVisa")
            {

                var calculatorId = int.Parse(context.Request["CalculatorId"]);
                GetVisa(context, calculatorId);

            }
            else if (!String.IsNullOrEmpty(context.Request["function"]) &&
         context.Request["function"] == "InsertVisaItem")
            {
                try
                {
                    var calculatorInfo = new CalculatorInfo();
                    calculatorInfo.ItemType = int.Parse(context.Request["TypeId"]);
                    calculatorInfo.TourId = int.Parse(context.Request["TourId"]);
                    calculatorInfo.ItemId = int.Parse(context.Request["ItemId"]);
                    calculatorInfo.PriceAdults = decimal.Parse(context.Request["PriceAdults"]);
                    calculatorInfo.PriceChild = decimal.Parse(context.Request["PriceChilds"]);
                    calculatorInfo.PriceBaby = decimal.Parse(context.Request["RriceBaby"]);

                    calculatorInfo.Vat = double.Parse(context.Request["Vat"]);
                    calculatorInfo.TranslateAdult = int.Parse(context.Request["TranslateAdult"]);
                    calculatorInfo.TranslateChild = int.Parse(context.Request["TranslateChild"]);

                    calculatorInfo.QuantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    calculatorInfo.QuantityChild = int.Parse(context.Request["QuantityChild"]);

                    calculatorInfo.OtherCostAdult = int.Parse(context.Request["OtherCostAdult"]);
                    calculatorInfo.OtherCostChild = int.Parse(context.Request["OtherCostChild"]);
                    calculatorInfo.OthercostBaby = int.Parse(context.Request["QtherCostBaby"]);
                    calculatorInfo.QuantityBaby = int.Parse(context.Request["QuantityBaby"]);
                    calculatorInfo.TranslateBaby = int.Parse(context.Request["TranslateBaby"]);

                    var quantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertVisa(context, calculatorInfo, calculatorId, className, quantityAdults, quantityChild);
                }
                catch
                {
                    var calculatorInfo = new CalculatorInfo();
                    var quantityAdults = int.Parse(context.Request["QuantityAdults"]);
                    var quantityChild = int.Parse(context.Request["QuantityChild"]);
                    var calculatorId = int.Parse(context.Request["CalculatorId"]);
                    var className = context.Request["ClassName"];
                    InsertVisa(context, calculatorInfo, calculatorId, className, quantityAdults, quantityChild);
                }

            }
            #endregion
        }

        #region Address
        private void ListNationalByContinent(HttpContext context, int continentId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstNational = new NationalController().ListNationalByContinent(continentId);
                if (lstNational.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstNational)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.NationalId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.NationalName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("is_error");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void ListCityByNational(HttpContext context, int nationalId)
        {
            var ctrl = new CityController();
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstCity = ctrl.ListCityByNational(nationalId);
                if (lstCity.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCity)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.CityId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.CityName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("is_error");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void loadLocationByCity(HttpContext context, int cityId)
        {
            var ctrl = new LocationController();
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstLocation = ctrl.ListLocation().Where(p => p.CityId == cityId).ToList();
                if (lstLocation.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstLocation)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.LocationId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.LocationName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("is_error");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void ListVisaByPartner(HttpContext context, int partnerId)
        {
            var ctrl = new VisaController();
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstVisa = ctrl.ListVisa().Where(p => p.PartnerId == partnerId).ToList();
                if (lstVisa.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstVisa)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.VisaId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.VisaName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("is_error");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void ListHotelByHotelType(HttpContext context, int hotelTypeId, int cityId)
        {
            var ctrl = new HotelController();
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstHotel = ctrl.ListHotel().Where(p => p.HotelTypeId == hotelTypeId && p.CityId == cityId).ToList();
                if (lstHotel.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstHotel)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.HotelId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.HotelFullName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("is_error");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void ListRestaurantByRestaurantType(HttpContext context, int restaurantTypeId, int cityId)
        {
            var ctrl = new RestaurantController();
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstRestaurant = ctrl.ListRestaurant().Where(p => p.RestaurantTypeId == restaurantTypeId && p.CityId == cityId).ToList();
                if (lstRestaurant.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstRestaurant)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.RestaurantId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.RestaurantFullName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("is_error");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void ListDistrictByCity(HttpContext context, int cityId)
        {
            var ctrl = new DistrictController();
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstDistrict = ctrl.ListDistrictByCity(cityId);
                if (lstDistrict.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstDistrict)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.DistrictId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.DistrictName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void ListWardByDistrict(HttpContext context, int districtId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstWard = new WardController().ListWardByDistrict(districtId);
                if (lstWard.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstWard)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.WardId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.WardName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        #endregion
         
        #region TravelGuidePrice
        private void ListTravelGuidePriceByTravelGuide(HttpContext context, int travelGuideId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstTravelGuidePrice = new TravelGuidePriceController().ListTravelGuidePriceByTravelGuide(travelGuideId);
                if (lstTravelGuidePrice.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstTravelGuidePrice)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.TravelGuidePriceId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.TravelGuidePriceName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        #endregion

        #region Price

        private void ListPriceByType(HttpContext context, int typeId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var price = new PriceController().ListPriceByType(typeId);
                if (price.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in price)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.PriceId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.PriceName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void TicketListByConcernAndType(HttpContext context, int typeId, int concernId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var tickets = new TicketController().TicketListByConcern(typeId, concernId);
                if (tickets.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in tickets)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.TicketId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.PriceName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }


        private void ListRoomByHotel(HttpContext context, int hotelId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var room = new RoomController().ListRoomByHotel(hotelId);
                if (room.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in room)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.RoomId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.RoomName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void ListRoomTypeByHotel(HttpContext context, int hotelId, string season)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var roomType = new RoomTypeController().ListRoomTypeByHotel(hotelId, season);
                if (roomType.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in roomType)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.PriceId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.RoomTypeName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }


        private void ListInsuranceByPartner(HttpContext context, int partnerId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var insurance = new InsuranceController().ListInsuranceByPartner(partnerId);
                if (insurance.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in insurance)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.InsuranceId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.InsuranceName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void ListServiceByLocation(HttpContext context, int partnerId, int locationId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var service = new ServiceController().ListServiceByPartnerAndLocation(partnerId, locationId);
                if (service.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);
                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in service)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.ServiceId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.ServiceName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void ListMenuByRestaurantAndMenuType(HttpContext context, int restaurantId, int menuTypeId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var menu = new MenuController().ListMenuByRestaurantAndMenuType(restaurantId, menuTypeId);
                if (menu.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in menu)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.MenuId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.MenuName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void ListConcern(HttpContext context)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var concerns = new ConcernController().ListConcernByStatus((int)Globals.DocumentStatus.Use);
                if (concerns.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in concerns)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.ConcernId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.ConcernFullName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void ListPartner(HttpContext context)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var partner = new PartnerController().ListPartnerByStatus((int)Globals.DocumentStatus.Use);
                if (partner.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in partner)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.PartnerId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.PartnerName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void ListService(HttpContext context)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var service = new ServiceController().ListServiceByStatus((int)Globals.DocumentStatus.Use);
                if (service.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in service)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.ServiceId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.ServiceName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        #endregion
        #region PriceItem
        private void ListPriceBySchedule(HttpContext context, int Type, int scheduleId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var priceList = new PriceController().ListPriceBySchedule(Type, scheduleId);
                if (priceList.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in priceList)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.PriceId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.PriceName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        #endregion

        public void UploadImageAjax(HttpContext context, Image imageData, int imageSize)
        {
            const int maxWidth = 800;
            var logicalPath = "/upload/" + DateTime.Now.ToString("yyyy/MM/dd") + "/"; //Thư mục chứa hình ảnh
            var imageName = ImageUpload.GetUniqueFileName(); //Lấy tên file hình ảnh duy nhất
            var imgScale = ImageUpload.ScaleImage(imageData, maxWidth); //Resize ảnh trước khi lưu
            var imageUrl = ImageUpload.SaveImage(logicalPath, imgScale, imageName); //Lưu hình ảnh vào thư mục gốc

            const string authorName = "";
            var waterMark = imageSize >= 400;
            var imageUrlScale = ImageUpload.GetImageUrl(imageSize, imageUrl, waterMark, authorName); //Lưu hình ảnh vào thư mục sau khi resize

            var writer = new Jayrock.Json.JsonTextWriter();
            writer.WriteStartObject();
            writer.WriteMember("isError");
            writer.WriteBoolean(false);
            writer.WriteMember("imageUrl");
            writer.WriteString(imageUrl);
            writer.WriteMember("imageUrlScale");
            writer.WriteString(imageUrlScale);
            writer.WriteEndObject();
            context.Response.Write(writer.ToString());
        }

        private void ListPriceByHotelAndType(HttpContext context, int hotelId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstprice = new PriceController().ListPriceByHotelAndType(hotelId);
                if (lstprice.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstprice)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.PriceId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.PriceNameByHotel);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void ListLocationByScheduleType(HttpContext context, int scheduleType)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstLocation = new LocationController().ListLocationByScheduleType(scheduleType);
                if (lstLocation.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstLocation)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.LocationId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.LocationName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void ListLocationByLocationType(HttpContext context, int locationType)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstLocation = new LocationController().ListLocationByLocationType(locationType);
                if (lstLocation.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstLocation)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.LocationId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.LocationName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void ListCityandPartnerByNational(HttpContext context, int nationalId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstCity = new CityController().ListCityByNational(nationalId);
                var lstPartner = new PartnerController().ListPartner().Where(p => p.NationalId == nationalId);
                if (lstCity.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("arrayCity");
                    writer.WriteStartArray();
                    foreach (var x1 in lstCity)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("IDCity");
                        writer.WriteString(x1.CityId.ToString());
                        writer.WriteMember("NameCity");
                        writer.WriteString(x1.CityName);
                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();

                    writer.WriteMember("arrayPartner");
                    writer.WriteStartArray();
                    foreach (var x2 in lstPartner)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("IDPartner");
                        writer.WriteString(x2.PartnerId.ToString());
                        writer.WriteMember("NamePartner");
                        writer.WriteString(x2.PartnerName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void ListPriceBy(HttpContext context, int type, int scheduleId, DateTime date, string flightArrival, int concernId, string season)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {


                var lstPrice = new PriceController().ListPriceBy(type, scheduleId, date, flightArrival, concernId, season);
                if (lstPrice.Count > 0)
                {


                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstPrice)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.PriceId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.PriceName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                } 

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        public bool IsReusable
        {
            get { return true; }
        }

        #region chiet tinh

        private void GetPrice(HttpContext context, int priceId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var price = new PriceController().GetPrice(priceId);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);



                writer.WriteMember("PriceAdult");
                writer.WriteString(price.SalePrice.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(price.SalePriceChild.ToString());
                writer.WriteMember("PriceBaby");
                writer.WriteString(price.PriceBaby.ToString());
                writer.WriteEndObject();


                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void InsertAir(HttpContext context, CalculatorInfo calculatorInfo, int quantityAdult, int quantityChild, int calculatorId, string className)
        {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {
                if (className == "btnDeleteAir")
                {

                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);
                }

            }
            else
            {
                if (className == "btnDeleteAir")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);
                }

            }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {


                var lstCalculator =
                    new CalculatorController().ListCalculator()
                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == calculatorInfo.ItemType)
                        .ToList();
                var totalTicketAirPricesAdults = lstCalculator.Sum(p => p.TotalPriceAirAdults);
                var totalTicketAirPricesChild = lstCalculator.Sum(p => p.TotalPriceAirChild);
                var totalTicketAirPrices = totalTicketAirPricesAdults + totalTicketAirPricesChild;
                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);
                    writer.WriteMember("TotalTicketAirPricesAdults");
                    writer.WriteString(totalTicketAirPricesAdults.ToString());
                    writer.WriteMember("TotalTicketAirPricesChild");
                    writer.WriteString(totalTicketAirPricesChild.ToString());
                    writer.WriteMember("TotalTicketAirPrices");
                    writer.WriteString(totalTicketAirPrices.ToString());

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());
                        writer.WriteMember("ScheduleName");
                        writer.WriteString(x.ScheduleName.ToString());
                        writer.WriteMember("DateBegin");
                        writer.WriteString(x.DateBegin.ToString());
                        writer.WriteMember("FlightArrivals");
                        writer.WriteString(x.FlightArrivals.ToString());
                        writer.WriteMember("ConcernFullName");
                        writer.WriteString(x.ConcernFullName.ToString());
                        writer.WriteMember("TicketName");
                        writer.WriteString(x.TicketName.ToString());
                        writer.WriteMember("PriceAdult");
                        writer.WriteString(x.PriceAdults.ToString());
                        writer.WriteMember("PriceChild");
                        writer.WriteString(x.PriceChild.ToString());
                        writer.WriteMember("VatAir");
                        writer.WriteString(x.Vat.ToString());
                        writer.WriteMember("TotalPriceAir");
                        writer.WriteString(x.TotalPriceAir.ToString());

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }




        private void InsertStation(HttpContext context, CalculatorInfo calculatorInfo, int calculatorId, string className)
        {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {
                if (className == "btnDeleteStation")
                {

                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);
                }

            }
            else
            {
                if (className == "btnDeleteStation")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);
                }

            }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {


                var lstCalculator =
                    new CalculatorController().ListCalculator()
                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == calculatorInfo.ItemType)
                        .ToList();
                var totalTicketStationPricesAdults = lstCalculator.Sum(p => p.TotalPriceStationAdults);
                var totalTicketStationPricesChild = lstCalculator.Sum(p => p.TotalPriceStationChild);
                var totalTicketStationPrices = totalTicketStationPricesAdults + totalTicketStationPricesChild;
                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("TotalTicketStationPricesAdults");
                    writer.WriteString(totalTicketStationPricesAdults.ToString());
                    writer.WriteMember("TotalTicketStationPricesChild");
                    writer.WriteString(totalTicketStationPricesChild.ToString());
                    writer.WriteMember("TotalTicketStationPrices");
                    writer.WriteString(totalTicketStationPrices.ToString());

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());
                        writer.WriteMember("ScheduleName");
                        writer.WriteString(x.ScheduleName.ToString());
                        writer.WriteMember("DateBegin");
                        writer.WriteString(x.DateBegin.ToString());
                        writer.WriteMember("FlightArrivals");
                        writer.WriteString(x.FlightArrivals.ToString());
                        writer.WriteMember("ConcernFullName");
                        writer.WriteString(x.ConcernFullName.ToString());
                        writer.WriteMember("TicketName");
                        writer.WriteString(x.TicketName.ToString());
                        writer.WriteMember("PriceAdult");
                        writer.WriteString(x.PriceAdults.ToString());
                        writer.WriteMember("PriceChild");
                        writer.WriteString(x.QuantityAdults.ToString());
                        writer.WriteMember("QuantityAdult");
                        writer.WriteString(x.QuantityChild.ToString());
                        writer.WriteMember("QuantityChild");
                        writer.WriteString(x.PriceChild.ToString());
                        writer.WriteMember("VatStation");
                        writer.WriteString(x.Vat.ToString());
                        writer.WriteMember("TotalPriceStation");
                        writer.WriteString(x.TotalPriceStation.ToString());

                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }


        private void GetTicketAir(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var calculator = new CalculatorController().GetCalculator(calculatorId);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);



                writer.WriteMember("ScheduleId");
                writer.WriteString(calculator.ScheduleId.ToString());
                writer.WriteMember("DateBegin");
                writer.WriteString(calculator.DateBegin.ToString());
                writer.WriteMember("FlightArrivals");
                writer.WriteString(calculator.FlightArrivals);
                writer.WriteMember("ConcernId");
                writer.WriteString(calculator.ConcernId.ToString());
                writer.WriteMember("TicketId");
                writer.WriteString(calculator.ItemId.ToString());
                writer.WriteMember("PriceAdult");
                writer.WriteString(calculator.PriceAdults.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(calculator.PriceChild.ToString());
                writer.WriteMember("VatAir");
                writer.WriteString(calculator.Vat.ToString());
                writer.WriteMember("TotalPriceAir");
                writer.WriteString(calculator.TotalPriceAir.ToString());
                writer.WriteEndObject();


                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void GetTicketStation(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var calculator = new CalculatorController().GetCalculator(calculatorId);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);
                writer.WriteMember("ScheduleId");
                writer.WriteString(calculator.ScheduleId.ToString());
                writer.WriteMember("DateBegin");
                writer.WriteString(calculator.DateBegin.ToString());
                writer.WriteMember("FlightArrivals");
                writer.WriteString(calculator.FlightArrivals);
                writer.WriteMember("ConcernId");
                writer.WriteString(calculator.ConcernId.ToString());
                writer.WriteMember("TicketId");
                writer.WriteString(calculator.ItemId.ToString());
                writer.WriteMember("PriceAdult");
                writer.WriteString(calculator.PriceAdults.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(calculator.PriceChild.ToString());
                writer.WriteMember("QuantityAdult");
                writer.WriteString(calculator.QuantityAdults.ToString());
                writer.WriteMember("QuantityChild");
                writer.WriteString(calculator.QuantityChild.ToString());
                writer.WriteMember("VatStation");
                writer.WriteString(calculator.Vat.ToString());
                writer.WriteMember("TotalPriceStation");
                writer.WriteString(calculator.TotalPriceStation.ToString());

                writer.WriteEndObject();


                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        #endregion

        #region chiet tinh Car
        private void ListPriceByCar(HttpContext context, int type, int scheduleCar, DateTime date, string soChoCar, int concernCar, int yearCar, string seasionCar)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {

                var lstPrice = new PriceController().ListPriceByCar(type, scheduleCar, date, soChoCar, concernCar, yearCar, seasionCar);
                if (lstPrice.Count > 0)
                {
                    var priceInfo = lstPrice.First();


                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);
                    writer.WriteMember("PriceCar");
                    writer.WriteString(priceInfo.ScheduleName.ToString());
                    writer.WriteMember("PriceId");
                    writer.WriteString(priceInfo.PriceId.ToString());
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void InsertCar(HttpContext context, CalculatorInfo calculatorInfo, int calculatorId, string className, int quantityAdults, int quantityChild)
        {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {
                if (className == "btnDeleteCar")
                {

                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);
                }

            }
            else
            {
                if (className == "btnDeleteCar")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);
                }

            }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {


                var lstCalculator =
                    new CalculatorController().ListCalculator()
                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == calculatorInfo.ItemType)
                        .ToList();

                var totalTicketCarPrices = lstCalculator.Sum(p => p.TotalPriceCar);
                var priceCarOnCustomer = totalTicketCarPrices / (quantityAdults + quantityChild);
                var totalTicketCarPricesAdults = priceCarOnCustomer * quantityAdults;
                var totalTicketCarPricesChild = priceCarOnCustomer * quantityChild;
                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("TotalTicketCarPrices");
                    writer.WriteString(totalTicketCarPrices.ToString());
                    writer.WriteMember("TotalTicketCarPricesAdults");
                    writer.WriteString(totalTicketCarPricesAdults.ToString());
                    writer.WriteMember("TotalTicketCarPricesChild");
                    writer.WriteString(totalTicketCarPricesChild.ToString());

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());
                        writer.WriteMember("ScheduleName");
                        writer.WriteString(x.ScheduleName.ToString());
                        writer.WriteMember("DateBegin");
                        writer.WriteString(x.DateBegin.ToString());
                        writer.WriteMember("QuantityCar");
                        writer.WriteString(x.QuantityCar.ToString());
                        writer.WriteMember("SoCho");
                        writer.WriteString(x.Time.ToString());
                        writer.WriteMember("ConcernFullName");
                        writer.WriteString(x.ConcernFullName.ToString());
                        writer.WriteMember("YearCar");
                        writer.WriteString(x.TicketType.ToString());
                        writer.WriteMember("PriceAdult");
                        writer.WriteString(x.PriceAdults.ToString());
                        writer.WriteMember("TotalPriceCar");
                        writer.WriteString(((double.Parse(x.PriceAdults.ToString()) * double.Parse(x.QuantityCar.ToString())) + (double.Parse(x.PriceAdults.ToString()) * double.Parse(x.QuantityCar.ToString())) * (x.Vat / 100)).ToString());
                        writer.WriteMember("VatCar");
                        writer.WriteString((x.Vat).ToString());

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void GetTicketCar(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var calculator = new CalculatorController().GetCalculator(calculatorId);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);



                writer.WriteMember("ScheduleId");
                writer.WriteString(calculator.ScheduleId.ToString());
                writer.WriteMember("DateBegin");
                writer.WriteString(calculator.DateBegin.ToString());
                writer.WriteMember("QuantityCar");
                writer.WriteString(calculator.QuantityCar.ToString());
                writer.WriteMember("ConcernId");
                writer.WriteString(calculator.ConcernId.ToString());
                writer.WriteMember("SoCho");
                writer.WriteString(calculator.Time.ToString());
                writer.WriteMember("YearCar");
                writer.WriteString(calculator.TicketType.ToString());
                writer.WriteMember("PriceCar");
                writer.WriteString(calculator.PriceAdults.ToString());
                writer.WriteMember("PriceId");
                writer.WriteString(calculator.ItemId.ToString());
                writer.WriteMember("TotalPriceCar");
                writer.WriteString(((double.Parse(calculator.PriceAdults.ToString()) * double.Parse(calculator.QuantityCar.ToString())) + (double.Parse(calculator.PriceAdults.ToString()) * double.Parse(calculator.QuantityCar.ToString())) * (calculator.Vat / 100)).ToString());
                writer.WriteMember("VatCar");
                writer.WriteString(calculator.Vat.ToString());

                writer.WriteEndObject();


                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        #endregion


        #region chiet tinh khach san
        private void PriceRoom(HttpContext context, int roomId, DateTime dateBegin)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var priceRoom = new PriceController().GetPriceByRoom(roomId, dateBegin);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);
                writer.WriteMember("PriceAdult");
                writer.WriteString(priceRoom.SalePrice.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(priceRoom.SalePriceChild.ToString());
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void InsertCalculatorHotel(CalculatorInfo calculatorInfo)
        {
            var calculatorController = new CalculatorController();
            calculatorController.InsertCalculator(calculatorInfo);

        }
      
        private void UpdateCalculatorHotel(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var calculator = new CalculatorController().GetCalculator(calculatorId);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);

                writer.WriteMember("HotelTypeId");
                writer.WriteString(calculator.HotelTypeId.ToString());
                writer.WriteMember("CityId");
                writer.WriteString(calculator.CityId.ToString());
                writer.WriteMember("Nationalid");
                writer.WriteString(calculator.Nationalid.ToString());
                
                writer.WriteMember("HotelId");
                writer.WriteString(calculator.HotelId.ToString());
                writer.WriteMember("PriceId");
                writer.WriteString(calculator.PriceId.ToString());
                writer.WriteMember("BeginDate");
                writer.WriteString(calculator.DateBegin.ToShortDateString().ToString());
                writer.WriteMember("EndDate");
                writer.WriteString(calculator.DateEnd.ToShortDateString().ToString());
                writer.WriteMember("PriceAdults");
                writer.WriteString(calculator.PriceAdults.ToString("#,###", new CultureInfo("vi-VN")));
                writer.WriteMember("PriceChild");
                writer.WriteString(calculator.PriceChild.ToString("#,###", new CultureInfo("vi-VN")));
                writer.WriteMember("QuantityAdults");
                writer.WriteString(calculator.QuantityAdults.ToString());
                writer.WriteMember("QuantityChild");
                writer.WriteString(calculator.QuantityChild.ToString());
                writer.WriteMember("Vat");
                writer.WriteString(calculator.Vat.ToString());
                writer.WriteMember("TotalPriceMenu");
                writer.WriteString(((double.Parse(calculator.TotalPriceHotelAdults.ToString()) + double.Parse(calculator.TotalPriceHotelChild.ToString()))).ToString("#,###", new CultureInfo("vi-VN")));
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }

        }
        #endregion

        #region chiet tinh an uong
        private void PriceListByMenu(HttpContext context, int type, int restaurantId, int menuTypeId, string seasonTour, DateTime date)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {

                var lstPrice = new PriceController().ListPriceByMenu(type, restaurantId, menuTypeId, seasonTour, date);
                if (lstPrice.Count > 0)
                {

                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstPrice)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("PriceMenu");
                        writer.WriteString(x.MenuName.ToString());
                        writer.WriteMember("PriceId");
                        writer.WriteString(x.PriceId.ToString());
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void InsertCalculatorHotel(HttpContext context, CalculatorInfo calculatorInfo, int calculatorId, string className)
        {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {

                if (className == "btnDeleteHotel")
                {

                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);
                }

            }
            else
            {
                if (className == "btnDeleteHotel")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);
                }

            }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstCalculator =
                    new CalculatorController().ListCalculator()
                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == calculatorInfo.ItemType)
                        .ToList();
                var totalTicketHotelPricesAdults = lstCalculator.Sum(p => p.TotalPriceHotelAdults);
                var totalTicketHotelPricesChild = lstCalculator.Sum(p => p.TotalPriceHotelChild);
                var totalTicketHotelPrices = totalTicketHotelPricesAdults + totalTicketHotelPricesChild;


                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("TotalTicketHotelPrices");
                    writer.WriteString(totalTicketHotelPrices.ToString());
                    writer.WriteMember("TotalTicketHotelPricesAdults");
                    writer.WriteString(totalTicketHotelPricesAdults.ToString());
                    writer.WriteMember("TotalTicketHotelPricesChild");
                    writer.WriteString(totalTicketHotelPricesChild.ToString());

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());
                        writer.WriteMember("HotelType");
                        writer.WriteString(x.HotelTypeName);
                        writer.WriteMember("National");
                        writer.WriteString(x.NationalName);
                        writer.WriteMember("City");
                        writer.WriteString(x.CityName);
                        writer.WriteMember("Hotel");
                        writer.WriteString(x.HotelFullName);
                        writer.WriteMember("DateBegin");
                        writer.WriteString(x.DateBegin.ToString());
                        writer.WriteMember("DateEnd");
                        writer.WriteString(x.DateEnd.ToString());
                        writer.WriteMember("CountNight");
                        writer.WriteString(x.CountNightHotel.ToString());
                        writer.WriteMember("RoomType");
                        writer.WriteString(x.RoomTypeName);
                        writer.WriteMember("QuantityAdult");
                        writer.WriteString(x.QuantityAdults.ToString());
                        writer.WriteMember("QuantityChild");
                        writer.WriteString(x.QuantityChild.ToString());
                        writer.WriteMember("PriceAdult");
                        writer.WriteString(x.PriceAdults.ToString());
                        writer.WriteMember("PriceChild");
                        writer.WriteString(x.PriceChild.ToString());
                        writer.WriteMember("VAT");
                        writer.WriteString(x.Vat.ToString());
                        writer.WriteMember("Total");
                        writer.WriteString((x.TotalPriceHotelAdults + x.TotalPriceHotelChild).ToString());

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }

        }

        private void InsertMenu(HttpContext context, CalculatorInfo calculatorInfo, int calculatorId, string className)
        {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {
                if (className == "btnDeleteMenu")
                {

                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);
                }

            }
            else
            {
                if (className == "btnDeleteMenu")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);
                }

            }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstCalculator =
                    new CalculatorController().ListCalculator()
                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == calculatorInfo.ItemType)
                        .ToList();

               
                //var PriceMenuAdults = lstCalculator.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                //var PriceMenuChild = lstCalculator.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                //var PriceMenuBaby = lstCalculator.Sum(p => ((p.PriceBaby * p.QuantityBaby) + (p.PriceBaby * p.QuantityBaby) * Convert.ToDecimal(p.Vat / 100)));
              
                //var TotalMenuPrices = PriceMenuAdults + PriceMenuChild + PriceMenuBaby;
                //var TotalMenuAdults = PriceMenuAdults / lstCalculator.Sum(p => p.QuantityAdults);
                //var TotalMenuChild = PriceMenuChild / lstCalculator.Sum(p => p.QuantityChild);
                //var TotalMenuBaby = PriceMenuBaby / lstCalculator.Sum(p => p.QuantityBaby);


                PriceAdults = lstCalculator.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                PriceChild = lstCalculator.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                PriceBaby = lstCalculator.Sum(p => ((p.PriceBaby * p.QuantityBaby) + (p.PriceBaby * p.QuantityBaby) * Convert.ToDecimal(p.Vat / 100)));
                CountItem = lstCalculator.Count();
                var TotalMenuAdults = LoadTongNguoi(lstCalculator, 1);
                var TotalMenuChild = LoadTongNguoi(lstCalculator, 2);
                var TotalMenuBaby = LoadTongNguoi(lstCalculator, 3);
                var TotalMenuPrices = PriceAdults + PriceChild + PriceBaby;
                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);
                    writer.WriteMember("TotalMenuPrices");
                    writer.WriteString(TotalMenuPrices.ToString());
                    writer.WriteMember("TotalMenuAdults");
                    writer.WriteString(TotalMenuAdults.ToString());
                    writer.WriteMember("TotalMenuChild");
                    writer.WriteString(TotalMenuChild.ToString());
                    writer.WriteMember("TotalMenuBaby");
                    writer.WriteString(TotalMenuBaby.ToString());
                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());
                        writer.WriteMember("RestaurantTypeName");
                        writer.WriteString(x.RestaurantTypeName.ToString());
                        writer.WriteMember("NationalName");
                        writer.WriteString(x.NationalName.ToString());
                        writer.WriteMember("CityName");
                        writer.WriteString(x.CityName.ToString());
                        writer.WriteMember("RestaurantFullName");
                        writer.WriteString(x.RestaurantFullName.ToString());
                        writer.WriteMember("DateBegin");
                        writer.WriteString(x.DateBegin.ToString());
                        writer.WriteMember("MenuTypeName");
                        writer.WriteString(x.MenuTypeName.ToString());
                        writer.WriteMember("MenuName");
                        writer.WriteString(x.MenuName.ToString());
                        writer.WriteMember("PriceAdults");
                        writer.WriteString(x.PriceAdults.ToString());

                        writer.WriteMember("PriceChild");
                        writer.WriteString(x.PriceChild.ToString());

                        writer.WriteMember("PriceBaby");
                        writer.WriteString(x.PriceBaby.ToString());

                        writer.WriteMember("QuantityAdults");
                        writer.WriteString(x.QuantityAdults.ToString());
                        writer.WriteMember("QuantityChild");
                        writer.WriteString(x.QuantityAdults.ToString());
                        writer.WriteMember("QuantityBaby");
                        writer.WriteString(x.QuantityBaby.ToString());
                        writer.WriteMember("TotalPriceMenu");
                        writer.WriteString(((double.Parse(x.TotalPriceMenuAdults.ToString()) + double.Parse(x.TotalPriceMenuChild.ToString()) + double.Parse(x.TotalPriceMenuBaby.ToString()).ToString())));
                        writer.WriteMember("Vat");
                        writer.WriteString((x.Vat).ToString());

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void GetTicketMenu(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var calculator = new CalculatorController().GetCalculator(calculatorId);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);

                writer.WriteMember("RestaurantId");
                writer.WriteString(calculator.RestaurantId.ToString());
                writer.WriteMember("CityId");
                writer.WriteString(calculator.CityId.ToString());
                writer.WriteMember("Nationalid");
                writer.WriteString(calculator.Nationalid.ToString());

                writer.WriteMember("MenuTypeId");
                writer.WriteString(calculator.MenuTypeId.ToString());
                writer.WriteMember("MenuId");
                writer.WriteString(calculator.ItemId.ToString());
                writer.WriteMember("RestaurantTypeId");
                writer.WriteString(calculator.RestaurantTypeId.ToString());
                writer.WriteMember("DateBegin");
                writer.WriteString(calculator.DateBegin.ToString());
                writer.WriteMember("PriceAdults");
                writer.WriteString(calculator.PriceAdults.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(calculator.PriceChild.ToString());
                writer.WriteMember("PriceBaby");

                writer.WriteString(calculator.PriceBaby.ToString());
                writer.WriteMember("QuantityAdults");
                writer.WriteString(calculator.QuantityAdults.ToString());
                writer.WriteMember("QuantityChild");
                writer.WriteString(calculator.QuantityChild.ToString());
                writer.WriteMember("QuantityBaby");
                writer.WriteString(calculator.QuantityBaby.ToString());
                writer.WriteMember("Vat");
                writer.WriteString(calculator.Vat.ToString());
                writer.WriteMember("TotalPriceMenu");
                writer.WriteString(
                    (
                    double.Parse(calculator.TotalPriceMenuAdults.ToString()) + double.Parse(calculator.TotalPriceMenuChild.ToString()) + double.Parse(calculator.TotalPriceMenuBaby.ToString()
                    ).ToString()
                    ));
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }

        }





        #endregion

        #region chiet tinh Dich vu khac

        private void PriceListByDvk(HttpContext context, int type, int partnerId, int locationId, string seasonTour)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstPrice = new PriceController().ListPriceByDvk(type, partnerId, locationId, seasonTour);
                if (lstPrice.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstPrice)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ServiceName");
                        writer.WriteString(x.ServiceName.ToString());
                        writer.WriteMember("PriceId");
                        writer.WriteString(x.PriceId.ToString());
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }



            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void  InsertDvk(HttpContext context, CalculatorInfo calculatorInfo, int calculatorId, string className, int quantityAdults, int quantityChild)
            {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {
                 if (className == "btnDeleteDvk")
                     {

                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);
                      }
                 }
            else
            {
                if (className == "btnDeleteDvk")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);
                     }
                }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                  var lstCalculator =
                    new CalculatorController().ListCalculatorDvk()
                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == calculatorInfo.ItemType)
                        .ToList();
              

                //var PriceDvkAdults = lstCalculator.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                //var PriceDvkChild = lstCalculator.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                //var PriceDvPaby = lstCalculator.Sum(p => ((p.PriceBaby * p.QuantityBaby) + (p.PriceBaby * p.QuantityBaby) * Convert.ToDecimal(p.Vat / 100)));
                //var TotalDvkPrices = PriceDvkAdults + PriceDvkChild + PriceDvPaby;
                //var TotalDvkAdults = PriceDvkAdults / lstCalculator.Sum(p => p.QuantityAdults);
                //var TotalDvkChild = PriceDvkChild / lstCalculator.Sum(p => p.QuantityChild);
                //var TotalDvkBaby = PriceDvPaby / lstCalculator.Sum(p => p.QuantityBaby);


                PriceAdults = lstCalculator.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                PriceChild = lstCalculator.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                PriceBaby = lstCalculator.Sum(p => ((p.PriceBaby * p.QuantityBaby) + (p.PriceBaby * p.QuantityBaby) * Convert.ToDecimal(p.Vat / 100)));
                CountItem = lstCalculator.Count();
                var TotalDvkPrices = PriceAdults + PriceChild + PriceBaby;
                var TotalDvkAdults = LoadTongNguoi(lstCalculator, 1);
                var TotalDvkChild = LoadTongNguoi(lstCalculator, 2);
                var TotalDvkBaby = LoadTongNguoi(lstCalculator, 3);
                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("TotalDvkPrices");
                    writer.WriteString(TotalDvkPrices.ToString());
                    writer.WriteMember("TotalDvkAdults");
                    writer.WriteString(TotalDvkAdults.ToString());
                    writer.WriteMember("TotalDvkChild");
                    writer.WriteString(TotalDvkChild.ToString());
                    writer.WriteMember("TotalDvkBaby");
                    writer.WriteString(TotalDvkBaby.ToString());

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());
                        writer.WriteMember("LocationTypeName");
                        writer.WriteString(x.LocationTypeName.ToString());
                        writer.WriteMember("NationalName");
                        writer.WriteString(x.NationalName.ToString());
                        writer.WriteMember("CityName");
                        writer.WriteString(x.CityName.ToString());
                        writer.WriteMember("LocationName");
                        writer.WriteString(x.LocationName.ToString());
                        writer.WriteMember("PartnerName");
                        writer.WriteString(x.PartnerName.ToString());
                        writer.WriteMember("ServiceName");
                        writer.WriteString(x.ServiceName.ToString());
                        writer.WriteMember("DateBegin");
                        writer.WriteString(x.DateBegin.ToString());
                        writer.WriteMember("PriceAdults");
                        writer.WriteString(x.PriceAdults.ToString());
                        writer.WriteMember("PriceChild");
                        writer.WriteString(x.PriceChild.ToString());
                        writer.WriteMember("QuantityAdults");
                        writer.WriteString(x.QuantityAdults.ToString());
                        writer.WriteMember("QuantityChild");
                        writer.WriteString(x.QuantityChild.ToString());

                        writer.WriteMember("PriceBaby");
                        writer.WriteString(x.PriceBaby.ToString());
                        writer.WriteMember("QuantityBaby");
                        writer.WriteString(x.QuantityBaby.ToString());
                        writer.WriteMember("TotalPriceMenu");
                        writer.WriteString(((double.Parse(x.TotalPriceDvkAdults.ToString()) + double.Parse(x.TotalPriceDvkChild.ToString()).ToString())));
                        writer.WriteMember("Vat");
                        writer.WriteString((x.Vat).ToString());

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
            }
        private void UpdateDvk(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var x = new CalculatorController().GetCalculatorDvk(calculatorId);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);


                writer.WriteMember("CalculatorId");
                writer.WriteString(x.CalculatorId.ToString());
                writer.WriteMember("LocationTypeId");
                writer.WriteString(x.LocationTypeId.ToString());
                writer.WriteMember("NationalId");
                writer.WriteString(x.NationalId.ToString());
                writer.WriteMember("CityId");
                writer.WriteString(x.CityId.ToString());
                writer.WriteMember("LocationId");
                writer.WriteString(x.LocationId.ToString());
                writer.WriteMember("PartnerId");
                writer.WriteString(x.PartnerId.ToString());
                writer.WriteMember("ServiceId");
                writer.WriteString(x.PriceId.ToString());
                writer.WriteMember("DateBegin");
                writer.WriteString(x.DateBegin.ToString());
                writer.WriteMember("PriceAdults");
                writer.WriteString(x.PriceAdults.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(x.PriceChild.ToString());
                writer.WriteMember("PriceBaby");
                writer.WriteString(x.PriceBaby.ToString());
                writer.WriteMember("QuantityAdults");
                writer.WriteString(x.QuantityAdults.ToString());
                writer.WriteMember("QuantityChild");
                writer.WriteString(x.QuantityChild.ToString());
                writer.WriteMember("QuantityBaby");
                writer.WriteString(x.QuantityBaby.ToString());
                writer.WriteMember("TotalPriceDvkAdults");
                writer.WriteString(x.TotalPriceDvkAdults.ToString());
                writer.WriteMember("TotalPriceDvkChild");
                writer.WriteString(x.TotalPriceDvkChild.ToString());
                writer.WriteMember("TotalPriceDvkBaby");
                writer.WriteString(x.TotalPriceDvkBaby.ToString());
                writer.WriteMember("Vat");
                writer.WriteString((x.Vat).ToString());

                writer.WriteEndObject();
                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }

        }



        #endregion












        #region chiet tinh HDV
        private void ListTravelGuideBy(HttpContext context, int language, string gender, string level)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstPrice = new TravelGuideCashController().ListTravelGuideBy(level, gender, language);
                if (lstPrice.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstPrice)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("ID");
                        writer.WriteString(x.TravelGuideCashId.ToString());
                        writer.WriteMember("Name");
                        writer.WriteString(x.TravelGuideName);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void GetTravelGuide(HttpContext context, int travelGuideId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var travelGuide = new TravelGuideCashController().GetTravelGuideCash(travelGuideId);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);
                writer.WriteMember("Price");
                writer.WriteString(travelGuide.Price.ToString());
                writer.WriteMember("PriceTip");
                writer.WriteString(travelGuide.ExtraMoney.ToString());

                writer.WriteEndObject();


                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }

        private void InsertTravelGuide(HttpContext context, CalculatorInfo calculatorInfo, int calculatorId, string className, int  quantityAdults, int quantityChild)
        {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {
                if (className == "btnDeleteTravelGuide")
                {
                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);
                }

            }
            else
            {
                if (className == "btnDeleteTravelGuide")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);
                }

            }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {

                var lstCalculator =
                                    new CalculatorController().ListCalculator()
                                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == calculatorInfo.ItemType)
                                        .ToList();



                var TotalTravelGuidePrices = (quantityAdults + quantityChild) != 0 ? Convert.ToInt32(lstCalculator.Sum(p => p.TotalPriceTravelGuide) / (quantityAdults + quantityChild)) : 0;
                CountItem = lstCalculator.Count();
                var TotalTravelGuidePricesAdults = TotalTravelGuidePrices * CountItem;
                var TotalTravelGuidePricesChild = TotalTravelGuidePrices * CountItem;
                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);
                    writer.WriteMember("TotalTravelGuidePrices");
                    writer.WriteString(TotalTravelGuidePrices.ToString());
                    writer.WriteMember("TotalTravelGuidePricesAdults");
                    writer.WriteString(TotalTravelGuidePricesAdults.ToString());
                    writer.WriteMember("TotalTravelGuidePricesChild");
                    writer.WriteString(TotalTravelGuidePricesChild.ToString());
                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("TravelGuideType");
                        writer.WriteString(x.TravelGuideType.ToString());
                        writer.WriteMember("BeginDate");
                        writer.WriteString(x.DateBegin.ToString());
                        writer.WriteMember("BeginEnd");
                        writer.WriteString(x.DateEnd.ToString());
                        writer.WriteMember("QuantityDate");
                        writer.WriteString((x.CountNightHotel + 1).ToString());
                        writer.WriteMember("LanguageName");
                        writer.WriteString(x.LanguageName.ToString());
                        writer.WriteMember("Gender");
                        writer.WriteString(x.Gender.ToString());
                        writer.WriteMember("Level");
                        writer.WriteString(x.Level.ToString());
                        writer.WriteMember("TravelGuideName");
                        writer.WriteString(x.TravelGuideName.ToString());
                        writer.WriteMember("Price");
                        writer.WriteString(x.PriceAdults.ToString());
                        writer.WriteMember("PriceTip");
                        writer.WriteString(x.PriceChild.ToString());
                        writer.WriteMember("Vat");
                        writer.WriteString(x.Vat.ToString());
                        writer.WriteMember("TotalPrice");
                        writer.WriteString(x.TotalPriceTravelGuide.ToString());
                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void UpdateTravelGuide(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var calculator = new CalculatorController().GetCalculator(calculatorId);
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);
                writer.WriteMember("TravelGuideType");
                writer.WriteString(calculator.TravelGuideType.ToString());
                writer.WriteMember("BeginDate");
                writer.WriteString(calculator.DateBegin.ToString());
                writer.WriteMember("BeginEnd");
                writer.WriteString(calculator.DateEnd.ToString());
                writer.WriteMember("QuantityDate");
                writer.WriteString((calculator.CountNightHotel + 1).ToString());
                writer.WriteMember("LanguageId");
                writer.WriteString(calculator.LanguageId.ToString());
                writer.WriteMember("Gender");
                writer.WriteString(calculator.Gender.ToString());
                writer.WriteMember("Level");
                writer.WriteString(calculator.Level.ToString());
                writer.WriteMember("TravelGuideCashId");
                writer.WriteString(calculator.TravelGuideCashId.ToString());
                writer.WriteMember("Price");
                writer.WriteString(calculator.PriceAdults.ToString());
                writer.WriteMember("PriceTip");
                writer.WriteString(calculator.PriceChild.ToString());
                writer.WriteMember("Vat");
                writer.WriteString(calculator.Vat.ToString());
                writer.WriteMember("TotalPrice");
                writer.WriteString(calculator.TotalPriceTravelGuide.ToString());
                writer.WriteMember("CalculatorId");
                writer.WriteString(calculator.CalculatorId.ToString());
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        #endregion

        #region chiet tinh bao hiem
        private void PriceInsurance(HttpContext context, int insuranceId, DateTime dateBegin)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var priceIn = new PriceController().GetPriceByInsurance(insuranceId, dateBegin);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);
                writer.WriteMember("PriceAdult");
                writer.WriteString(priceIn.SalePrice.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(priceIn.SalePriceChild.ToString());
                writer.WriteMember("PriceId");
                writer.WriteString(priceIn.PriceId.ToString());
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void InsertCalculatorInsurance(HttpContext context, CalculatorInfo calculatorInfo, int calculatorId, string className)
        {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {

                if (className == "btnDeleteInsurance")
                {

                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);
                }

            }
            else
            {
                if (className == "btnDeleteInsurance")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);
                }

            }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var lstCalculator =
                    new CalculatorController().ListCalculator()
                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == calculatorInfo.ItemType)
                        .ToList();
                var totalInsurancePricesAdults = lstCalculator.Sum(p => p.TotalPriceInsuranceAdults);
                var totalInsurancePricesChild = lstCalculator.Sum(p => p.TotalPriceInsuranceChild);
                var totalInsurancePrices = totalInsurancePricesAdults + totalInsurancePricesChild;


                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("TotalInsurancePrices");
                    writer.WriteString(totalInsurancePrices.ToString());
                    writer.WriteMember("TotalInsurancePricesAdults");
                    writer.WriteString(totalInsurancePricesAdults.ToString());
                    writer.WriteMember("TotalInsurancePricesChild");
                    writer.WriteString(totalInsurancePricesChild.ToString());

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                        writer.WriteStartObject();
                        writer.WriteMember("PartnerName");
                        writer.WriteString(x.PartnerName);
                        writer.WriteMember("InsuranceName");
                        writer.WriteString(x.InsuranceName);
                        writer.WriteMember("BeginDate");
                        writer.WriteString(x.DateBegin.ToString());
                        writer.WriteMember("EndDate");
                        writer.WriteString(x.DateEnd.ToString());
                        writer.WriteMember("QuantityDate");
                        writer.WriteString((x.CountNightHotel + 1).ToString());
                        writer.WriteMember("PriceAdult");
                        writer.WriteString(x.PriceAdults.ToString());
                        writer.WriteMember("PriceChild");
                        writer.WriteString(x.PriceChild.ToString());
                        writer.WriteMember("QuantityAdult");
                        writer.WriteString(x.QuantityAdults.ToString());
                        writer.WriteMember("QuantityChild");
                        writer.WriteString(x.QuantityChild.ToString());
                        writer.WriteMember("VAT");
                        writer.WriteString(x.Vat.ToString());
                        writer.WriteMember("TotalPrice");
                        writer.WriteString((x.TotalPriceInsuranceAdults + x.TotalPriceInsuranceChild).ToString());
                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void UpdateCalculatorInsurance(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var calculator = new CalculatorController().GetCalculator(calculatorId);
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);
                writer.WriteMember("PartnerId");
                writer.WriteString(calculator.PartnerId.ToString());
                writer.WriteMember("DateBegin");
                writer.WriteString(calculator.DateBegin.ToString());
                writer.WriteMember("DateEnd");
                writer.WriteString(calculator.DateEnd.ToString());
                writer.WriteMember("QuantityDate");
                writer.WriteString((calculator.CountNightHotel + 1).ToString());

                writer.WriteMember("InsuranceId");
                writer.WriteString(calculator.InsuranceId.ToString());
                writer.WriteMember("PriceAdult");
                writer.WriteString(calculator.PriceAdults.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(calculator.PriceChild.ToString());
                writer.WriteMember("QuantityAdult");
                writer.WriteString(calculator.QuantityAdults.ToString());
                writer.WriteMember("QuantityChild");
                writer.WriteString(calculator.QuantityChild.ToString());
                writer.WriteMember("Vat");
                writer.WriteString(calculator.Vat.ToString());
                writer.WriteMember("TotalPrice");
                writer.WriteString((calculator.TotalPriceInsuranceAdults + calculator.TotalPriceInsuranceChild).ToString());
                writer.WriteMember("CalculatorId");
                writer.WriteString(calculator.CalculatorId.ToString());
                writer.WriteMember("PriceId");
                writer.WriteString(calculator.ItemId.ToString());
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }

        }
        #endregion
         

        #region chiet tinh Hoa hong
         

        private void InsertHH(HttpContext context, CalculatorInfo calculatorInfo, int calculatorId, string className, int quantityAdults, int quantityChild)
        {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {
                if (className == "btnDeleteHH")
                {

                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);
                }

            }
            else
            {
                if (className == "btnDeleteHH")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);
                }

            }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {


                var lstCalculator =
                    new CalculatorController().ListCalculatorDvk()
                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == 11)
                        .ToList(); 

              //  var PricesHHAdults = lstCalculator.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
              //  var PricesHHChild = lstCalculator.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
              //  var PricesHHBaby = lstCalculator.Sum(p => ((p.PriceBaby * p.QuantityBaby) + (p.PriceBaby * p.QuantityBaby) * Convert.ToDecimal(p.Vat / 100)));

              //var TotalHHPrices = PricesHHAdults + PricesHHChild + PricesHHBaby;
              //var TotalHHAdults = PricesHHAdults / lstCalculator.Sum(p => p.QuantityBaby);
              //var TotalHHChild = PricesHHChild / lstCalculator.Sum(p => p.QuantityChild);
              //var TotalHHBaby = PricesHHBaby / lstCalculator.Sum(p => p.QuantityBaby);


                PriceAdults = lstCalculator.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                PriceChild = lstCalculator.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                PriceBaby = lstCalculator.Sum(p => ((p.PriceBaby * p.QuantityBaby) + (p.PriceBaby * p.QuantityBaby) * Convert.ToDecimal(p.Vat / 100)));
                CountItem = lstCalculator.Count();
              var TotalHHPrices = PriceAdults + PriceChild + PriceBaby;
              var TotalHHAdults = LoadTongNguoi(lstCalculator, 1);
              var TotalHHChild = LoadTongNguoi(lstCalculator, 2);
              var TotalHHBaby = LoadTongNguoi(lstCalculator, 3);
                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);
                    writer.WriteMember("TotalHHPrices");
                    writer.WriteString(TotalHHPrices.ToString());
                    writer.WriteMember("TotalHHAdults");
                    writer.WriteString(TotalHHAdults.ToString());
                    writer.WriteMember("TotalHHChild");
                    writer.WriteString(TotalHHChild.ToString());
                    writer.WriteMember("TotalHHBaby");
                    writer.WriteString(TotalHHBaby.ToString());

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                       
                        double Total = double.Parse(x.PriceAdults.ToString()) * double.Parse(x.QuantityAdults.ToString()) 
                            + double.Parse(x.PriceChild.ToString()) * double.Parse(x.QuantityChild.ToString())
                            + double.Parse(x.PriceBaby.ToString()) * double.Parse(x.QuantityBaby.ToString());
                        Total = Total +  Total*double.Parse(x.Vat.ToString()) /100;
                        writer.WriteStartObject();
                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());
                        writer.WriteMember("TravelGuideType");
                        writer.WriteString(x.TravelGuideType.ToString());
                        writer.WriteMember("PriceAdults");
                        writer.WriteString(x.PriceAdults.ToString());
                        writer.WriteMember("PriceChild");
                        writer.WriteString(x.PriceChild.ToString());
                        writer.WriteMember("QuantityAdults");
                        writer.WriteString(x.QuantityAdults.ToString());
                        writer.WriteMember("QuantityChild");
                        writer.WriteString(x.QuantityChild.ToString());
                        writer.WriteMember("TotalPriceHhAdults");
                        writer.WriteString(x.TotalPriceHhAdults.ToString());
                        writer.WriteMember("TotalPriceHhChild");
                        writer.WriteString(x.TotalPriceHhChild.ToString());
                        writer.WriteMember("TotalPriceHhBaby");
                        writer.WriteString(x.TotalPriceHhBaby.ToString());
                        writer.WriteMember("PriceBaby");
                        writer.WriteString(x.PriceBaby.ToString());
                        writer.WriteMember("QuantityBaby");
                        writer.WriteString(x.QuantityBaby.ToString());
                        writer.WriteMember("Vat");
                        writer.WriteString((x.Vat).ToString());
                        writer.WriteMember("Total");
                        writer.WriteString((Total).ToString());
                        writer.WriteMember("Vat");
                        writer.WriteString((x.Vat).ToString());

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                } 
            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }
        }
        private void UpdateHH(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var x = new CalculatorController().GetCalculatorDvk(calculatorId);

                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);
                double Total = double.Parse(x.PriceAdults.ToString()) * double.Parse(x.QuantityAdults.ToString())
                          + double.Parse(x.PriceChild.ToString()) * double.Parse(x.QuantityChild.ToString())
                          + double.Parse(x.PriceBaby.ToString()) * double.Parse(x.QuantityBaby.ToString());
                Total = Total + Total * double.Parse(x.Vat.ToString()) / 100;
                    
                writer.WriteMember("CalculatorId");
                writer.WriteString(x.CalculatorId.ToString());
                writer.WriteMember("TravelGuideType");
                writer.WriteString(x.TravelGuideType.ToString());
                writer.WriteMember("PriceAdults");
                writer.WriteString(x.PriceAdults.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(x.PriceChild.ToString());
                writer.WriteMember("QuantityAdults");
                writer.WriteString(x.QuantityAdults.ToString());
                writer.WriteMember("QuantityChild");
                writer.WriteString(x.QuantityChild.ToString());
                writer.WriteMember("PriceBaby");
                writer.WriteString(x.PriceBaby.ToString());
                writer.WriteMember("QuantityBaby");
                writer.WriteString(x.QuantityBaby.ToString());
                writer.WriteMember("Vat");
                writer.WriteString((x.Vat).ToString());
                writer.WriteMember("Total");
                writer.WriteString((Total).ToString());
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }

        } 
        #endregion 
































        #region chiet tinh Visa

        private void PriceListByVisa(HttpContext context, int partnerId, int visaTypeId)

        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {

                var lstPrice = new VisaPriceController().ListPriceByVisa(partnerId, visaTypeId);

                if (lstPrice.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstPrice)
                    {
                        writer.WriteStartObject();


                        writer.WriteMember("PriceVisa");
                        writer.WriteString(x.VisaName.ToString());
                        writer.WriteMember("PriceId");
                        writer.WriteString(x.VisaPriceId.ToString());


                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }



            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }


        }
        protected decimal PriceAdults = 0;
        protected decimal PriceChild = 0;
        protected decimal PriceBaby = 0;
        protected decimal CountItem = 0;
      
        private void InsertVisa(HttpContext context, CalculatorInfo calculatorInfo, int calculatorId, string className, int quantityAdults, int quantityChild)

        {
            var calculatorController = new CalculatorController();
            if (calculatorId == -1)
            {


                if (className == "btnDeleteVisa")

                {

                }
                else
                {
                    calculatorController.InsertCalculator(calculatorInfo);


                }

            }
            else
            {
                if (className == "btnDeleteVisa")
                {
                    calculatorInfo = calculatorController.GetCalculator(calculatorId);
                    calculatorController.DeleteCalculator(calculatorId);

                }
                else
                {
                    calculatorInfo.CalculatorId = calculatorId;
                    calculatorController.UpdateCalculator(calculatorInfo);


                }

            }
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {


                var lstCalculator =
                    new CalculatorController().ListCalculator()
                        .Where(p => p.TourId == calculatorInfo.TourId && p.ItemType == calculatorInfo.ItemType)
                        .ToList(); 
                //var PriceVisaAdults = lstCalculator.Sum(p => p.TotalPriceVisaAdults);
                //var PriceVisaChild = lstCalculator.Sum(p => p.TotalPriceVisaChild);
                //var PriceVisaBaby = lstCalculator.Sum(p => p.TotalPriceVisaBaby);

                //var TotalVisaPrices = PriceVisaAdults + PriceVisaChild;
                //var TotalVisaChild = PriceVisaAdults / lstCalculator.Sum(p => p.QuantityAdults);
                //var TotalVisaAdults = PriceVisaChild / lstCalculator.Sum(p => p.QuantityChild);
                //var TotalVisaBaby = PriceVisaBaby / lstCalculator.Sum(p => p.QuantityBaby);

                PriceAdults = lstCalculator.Sum(p => p.TotalPriceVisaAdults);
                PriceChild = lstCalculator.Sum(p => p.TotalPriceVisaChild);
                PriceBaby = lstCalculator.Sum(p => p.TotalPriceVisaBaby);
                 CountItem = lstCalculator.Count();
                var TotalVisaPrices = PriceAdults + PriceChild + PriceBaby;
                //var TotalVisaChild = LoadTongNguoi(lstCalculator, 2); //lstCalculator.Sum(p => p.QuantityAdults) != 0 ? (PriceVisaAdults / lstCalculator.Sum(p => p.QuantityAdults)) * CountItem : 0;
                //var TotalVisaAdults = LoadTongNguoi(lstCalculator, 1);// lstCalculator.Sum(p => p.QuantityChild) != 0 ? (PriceVisaChild / lstCalculator.Sum(p => p.QuantityChild)) * CountItem : 0;
                //var TotalVisaBaby = LoadTongNguoi(lstCalculator, 3);// lstCalculator.Sum(p => p.QuantityChild) != 0 ? (PriceVisaBaby / lstCalculator.Sum(p => p.QuantityBaby)) * CountItem : 0;
         
                var TotalVisaAdults = LoadTongNguoi(lstCalculator, 1);  
                var TotalVisaChild = LoadTongNguoi(lstCalculator, 2);
                var TotalVisaBaby = LoadTongNguoi(lstCalculator, 3);
                if (lstCalculator.Count > 0)
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(false);
                    writer.WriteMember("TotalVisaChild");
                    writer.WriteString(TotalVisaChild.ToString());

                    writer.WriteMember("TotalVisaAdults");
                    writer.WriteString(TotalVisaAdults.ToString());

                    writer.WriteMember("TotalVisaPrices");
                    writer.WriteString(TotalVisaPrices.ToString());

                    writer.WriteMember("TotalVisaBaby");
                    writer.WriteString(TotalVisaBaby.ToString());

                    writer.WriteMember("array");
                    writer.WriteStartArray();
                    foreach (var x in lstCalculator)
                    {
                        writer.WriteStartObject();

                        writer.WriteMember("CalculatorId");
                        writer.WriteString(x.CalculatorId.ToString());
                        writer.WriteMember("PartnerName");
                        writer.WriteString(x.PartnerName.ToString());
                        writer.WriteMember("VisaTypeName");
                        writer.WriteString(x.VisaTypeName.ToString());
                        writer.WriteMember("VisaName");
                        writer.WriteString(x.VisaName.ToString());
                        writer.WriteMember("PriceAdults");
                        writer.WriteString(x.PriceAdults.ToString());
                        writer.WriteMember("RriceBaby");
                        writer.WriteString(x.PriceBaby.ToString());
                        writer.WriteMember("PriceChild");
                        writer.WriteString(x.PriceChild.ToString());
                        writer.WriteMember("QuantityAdults");
                        writer.WriteString(x.QuantityAdults.ToString());
                        writer.WriteMember("QuantityChild");
                        writer.WriteString(x.QuantityChild.ToString());
                        writer.WriteMember("QuantityBaby");
                        writer.WriteString(x.QuantityBaby.ToString());
                        writer.WriteMember("TranslateAdult");
                        writer.WriteString(x.TranslateAdult.ToString());
                        writer.WriteMember("TranslateChild");
                        writer.WriteString(x.TranslateChild.ToString());
                        writer.WriteMember("TranslateBaby");
                        writer.WriteString(x.TranslateBaby.ToString());
                        writer.WriteMember("OtherCostAdult");
                        writer.WriteString(x.OtherCostAdult.ToString());
                        writer.WriteMember("OtherCostChild");
                        writer.WriteString(x.OtherCostChild.ToString());
                        writer.WriteMember("OtherCostBaby");
                        writer.WriteString(x.OthercostBaby.ToString());
                        writer.WriteMember("TotalPriceVisa");
                        writer.WriteString(((double.Parse(x.TotalPriceVisaAdults.ToString()) + double.Parse(x.TotalPriceVisaChild.ToString()).ToString())));
                        writer.WriteMember("Vat");
                        writer.WriteString((x.Vat).ToString());
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }
                else
                {
                    writer.WriteStartObject();
                    writer.WriteMember("isError");
                    writer.WriteBoolean(true);
                    writer.WriteEndObject();
                    context.Response.Write(writer.ToString());
                }


            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }

        }
        private void GetVisa(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var calculator = new CalculatorController().GetCalculator(calculatorId);


                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false);

                writer.WriteMember("VisaId");
                writer.WriteString(calculator.VisaId.ToString());
                writer.WriteMember("VisaPriceId");
                writer.WriteString(calculator.VisaPriceId.ToString());
                writer.WriteMember("PartnerId");
                writer.WriteString(calculator.PartnerId.ToString());
                writer.WriteMember("VisaTypeId");
                writer.WriteString(calculator.VisaTypeId.ToString());
                writer.WriteMember("PriceAdults");
                writer.WriteString(calculator.PriceAdults.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(calculator.PriceChild.ToString());
                writer.WriteMember("PriceChild");
                writer.WriteString(calculator.PriceChild.ToString());
                writer.WriteMember("RriceBaby");
                writer.WriteString(calculator.PriceBaby.ToString());
                
                writer.WriteMember("QuantityChild");
                writer.WriteString(calculator.QuantityChild.ToString());
                writer.WriteMember("QuantityAdults");
                writer.WriteString(calculator.QuantityAdults.ToString());
                writer.WriteMember("QuantityBaby");
                writer.WriteString(calculator.QuantityBaby.ToString());
                
                writer.WriteMember("VAT");
                writer.WriteString(calculator.Vat.ToString());
                writer.WriteMember("TranslateAdult");
                writer.WriteString(calculator.TranslateAdult.ToString());
                writer.WriteMember("TranslateChild");
                writer.WriteString(calculator.TranslateChild.ToString());
                writer.WriteMember("TranslateBaby");
                writer.WriteString(calculator.TranslateBaby.ToString());
                
                writer.WriteMember("OtherCostAdult");
                writer.WriteString(calculator.OtherCostAdult.ToString());
                writer.WriteMember("OtherCostChild");
                writer.WriteString(calculator.OtherCostChild.ToString());
                writer.WriteMember("QtherCostBaby");
                writer.WriteString(calculator.OthercostBaby.ToString());
                
                writer.WriteMember("TotalPriceVisa");
                writer.WriteString(((double.Parse(calculator.TotalPriceVisaAdults.ToString()) + double.Parse(calculator.TotalPriceVisaChild.ToString()))).ToString());


                writer.WriteEndObject();
                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }


        }
        private void GetVisaPrice(HttpContext context, int calculatorId)
        {
            var writer = new Jayrock.Json.JsonTextWriter();
            try
            {
                var calculator = new VisaPriceController().GetVisaPrice(calculatorId);


                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(false); 

                writer.WriteMember("Price");
                writer.WriteString(calculator.Price.ToString());
                writer.WriteMember("Stranlate");
                writer.WriteString(calculator.Stranlate.ToString());
                writer.WriteMember("OtherCosts");
                writer.WriteString(calculator.OtherCosts.ToString()); 

                writer.WriteEndObject();
                context.Response.Write(writer.ToString());

            }
            catch
            {
                writer.WriteStartObject();
                writer.WriteMember("isError");
                writer.WriteBoolean(true);
                writer.WriteEndObject();
                context.Response.Write(writer.ToString());
            }


        }

        protected int LoadTongNguoi(List<CalculatorInfo> ojb, int loai)
        {

            if (loai == 1) // người lơn
            {
                return Convert.ToInt32(ojb.Sum(p => p.QuantityAdults) != 0 ? (PriceAdults / ojb.Sum(p => p.QuantityAdults)) * CountItem : 0);
            }
            if (loai == 2) // người tre em
            {
                return Convert.ToInt32(ojb.Sum(p => p.QuantityChild) != 0 ? (PriceChild / ojb.Sum(p => p.QuantityChild)) * CountItem : 0);
            }
            if (loai == 3) // người tre nho
            {
                return Convert.ToInt32(ojb.Sum(p => p.QuantityBaby) != 0 ? (PriceBaby / ojb.Sum(p => p.QuantityBaby)) * CountItem : 0);
            }
            return 0;

        }
        #endregion

    }

}
